package Euler33;

import java.util.*;
import ReusableCode.MathFunctions;

public class DigitCancelingFractions {
	public static void main(String[] args) {
		List<int[]> fractions = new ArrayList<int[]>();
		
		for(int i = 11; i < 98; i++)
		{
			int numeratorDigits[] = MathFunctions.reverseArray(MathFunctions.getDigits(i));
			
			if(numeratorDigits[1]==0)
			{
				continue;
			}
			
			for(int j = i+1; j < 100; j++)
			{

				int denominatorDigits[] = MathFunctions.reverseArray(MathFunctions.getDigits(j));
				
				//Account for 0 case.
				if(denominatorDigits[1]==0)
				{
					continue;
				}
				
				double value = (double)i/j;
				
				//Check if the num and den have digits in common.
				int numCount = 0;
				while(numCount < 2)
				{
					int denCount = 0;
					int numDig = numeratorDigits[numCount];
					
					while(denCount < 2)
					{
						int denDig = denominatorDigits[denCount];
						
						if(numDig == denDig)
						{
							double val = (double) numeratorDigits[((numCount + 1)%2)] / denominatorDigits[((denCount + 1)%2)];
							
							if(val == value)
							{
								fractions.add(new int[]{i, j});
								//System.out.println(i + "\\" + j + " simplifies to " + numeratorDigits[((numCount + 1)%2)] + "\\" + denominatorDigits[((denCount + 1)%2)]);
							}
						}
						
						denCount++;
					}
					
					numCount++;
				}
			}
		}
		
		long a = 1, b = 1;
		for(int i = 0; i < fractions.size(); i++)
		{
			int vals[] = fractions.get(i);
			a *= vals[0];
			b *= vals[1];
		}
		
		
		System.out.println(b/MathFunctions.GCD(a, b));
	}
}
